package com.xunfang.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.xunfang.bean.House;
import com.xunfang.bean.dto.HouseDTO;
import com.xunfang.bean.vo.PageVo;
import com.xunfang.mapper.HouseMapper;
import com.xunfang.service.IHouseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author yellow Docter
 * @date 2023 -11-08
 * @desc
 */
@Service
public class HouseServiceImpl implements IHouseService {

    @Autowired
    private HouseMapper houseMapper;

    @Override
    public PageVo findHouses(Integer currentPage,Integer pageSize) {

        Page page = PageHelper.startPage(currentPage,pageSize).doSelectPage(()->{
                houseMapper.findHouseAll();
        });
        PageVo pageVo = new PageVo();
        pageVo.setTotal(page.getTotal());
        pageVo.setData(page.getResult());
        return pageVo;
    }

    @Override
    public boolean saveUpdateHouse(HouseDTO houseDTO) {

        //判断 添加 和修改操作
        //  int (基本数据类型、0) 和  Integer   (包装类,null)
        //houseDTO.getId() > 0
        //houseDTO.getId() == null
        if (houseDTO.getId() == null){
            //添加
            //设置默认值
            houseDTO.setStatus(0);
            houseDTO.setCreateTime(new Date());
            return houseMapper.saveHouse(houseDTO) > 0;

        }else{
            //修改
            return houseMapper.updateHouse(houseDTO) > 0;
        }

    }

    @Override
    public boolean deleteHouseById(Integer id) {
        return houseMapper.deleteHouseById(id) > 0;
    }

    @Override
    public List<House> findHousesByOwner(Integer id) {
        return houseMapper.findHousesByOwnerBy(id);
    }

    @Override
    public List<House> findHousesByBuildingId(Integer buildingId) {
        return houseMapper.findBuildByBuildingId(buildingId);
    }

}
